package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.TExternalPowerSupply;
import com.ruoyi.system.mapper.TExternalPowerSupplyMapper;
import com.ruoyi.system.service.ITExternalPowerSupplyService;

/**
 * 外部供电退服无停电告警Service业务层处理
 * 
 * @author zhangkai
 * @date 2021-12-10
 */
@Service
public class TExternalPowerSupplyServiceImpl implements ITExternalPowerSupplyService 
{
	
	private static final Logger log = LoggerFactory.getLogger(TExternalPowerSupplyServiceImpl.class);
	
    @Autowired
    private TExternalPowerSupplyMapper tExternalPowerSupplyMapper;

    /**
     * 查询外部供电退服无停电告警
     * 
     * @param id 外部供电退服无停电告警主键
     * @return 外部供电退服无停电告警
     */
    @Override
    public TExternalPowerSupply selectTExternalPowerSupplyById(String id)
    {
        return tExternalPowerSupplyMapper.selectTExternalPowerSupplyById(id);
    }

    /**
     * 查询外部供电退服无停电告警列表
     * 
     * @param tExternalPowerSupply 外部供电退服无停电告警
     * @return 外部供电退服无停电告警
     */
    @Override
    public List<TExternalPowerSupply> selectTExternalPowerSupplyList(TExternalPowerSupply tExternalPowerSupply)
    {
        return tExternalPowerSupplyMapper.selectTExternalPowerSupplyList(tExternalPowerSupply);
    }

    /**
     * 新增外部供电退服无停电告警
     * 
     * @param tExternalPowerSupply 外部供电退服无停电告警
     * @return 结果
     */
    @Override
    public int insertTExternalPowerSupply(TExternalPowerSupply tExternalPowerSupply)
    {
        tExternalPowerSupply.setCreateTime(DateUtils.getNowDate());
        tExternalPowerSupply.setId(UUID.randomUUID().toString().replace("-", ""));
        return tExternalPowerSupplyMapper.insertTExternalPowerSupply(tExternalPowerSupply);
    }

    /**
     * 修改外部供电退服无停电告警
     * 
     * @param tExternalPowerSupply 外部供电退服无停电告警
     * @return 结果
     */
    @Override
    public int updateTExternalPowerSupply(TExternalPowerSupply tExternalPowerSupply)
    {
        tExternalPowerSupply.setUpdateTime(DateUtils.getNowDate());
        return tExternalPowerSupplyMapper.updateTExternalPowerSupply(tExternalPowerSupply);
    }
    
    public int updateTExternalPowerSupplyForFengongshi(TExternalPowerSupply tExternalPowerSupply)
    {
    	tExternalPowerSupply.setReasonTime(DateUtils.getNowDate());

        return tExternalPowerSupplyMapper.updateTExternalPowerSupplyForFengongshi(tExternalPowerSupply);
    }

    /**
     * 批量删除外部供电退服无停电告警
     * 
     * @param ids 需要删除的外部供电退服无停电告警主键
     * @return 结果
     */
    @Override
    public int deleteTExternalPowerSupplyByIds(String[] ids)
    {
        return tExternalPowerSupplyMapper.deleteTExternalPowerSupplyByIds(ids);
    }

    /**
     * 删除外部供电退服无停电告警信息
     * 
     * @param id 外部供电退服无停电告警主键
     * @return 结果
     */
    @Override
    public int deleteTExternalPowerSupplyById(String id)
    {
        return tExternalPowerSupplyMapper.deleteTExternalPowerSupplyById(id);
    }
    
    /**
     * 导入全站数据
     *
     * @param totalList 全站数据列表
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importTotal(List<TExternalPowerSupply> totalList, String operName, String dateRange, String depName, String userName)
    {
        operName = SecurityUtils.getLoginUser().getUsername();
        Long userid = SecurityUtils.getLoginUser().getUserId();

        if((depName.contains("辽宁省") || "admin".equals(userName)) && dateRange.equals("null")) {
            throw new ServiceException("版本不能为空！");
        }
        String newDateRange = dateRange;//dateRange.replaceAll("-", "");
        if (StringUtils.isNull(totalList) || totalList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TExternalPowerSupply site : totalList)
        {
            try
            {
                site.setVersion(newDateRange);
                site.setUpdateBy(operName);
                if(!(depName.contains("辽宁省") || "admin".equals(userName))){
                    this.updateTExternalPowerSupplyForFengongshi(site);
                    site.setVersion(newDateRange);
                    site.setReasonBy(operName);
                }else if(StringUtils.isNull(site.getId())){
                    site.setUpdateBy(operName);
                    site.setUpdateTime(new Date());
                    this.insertTExternalPowerSupply(site);
                } else{
                    this.updateTExternalPowerSupply(site);
                }
                successNum++;
                //  successMsg.append("<br/>" + successNum + "、站址编码: " + site.getStationCode() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、站址编码: " + site.getStationCode() + " 导入失败：";
                failureMsg.append(msg + "系统异常");
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
